package com.hongguan.oms.web.pact;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.service.pact.PactService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;

/**
 * 
 * @author tonylin <br>
 * @create 2011-1-9 <br>
 * @project Haojin <br>
 * @copyright 2011 HongGuan Information. All rights reserved
 * @desc <br>
 */
public class PactHead extends BaseHandler {
	private PactService pactService;

	
	public ModelAndView djPact(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = new ModelAndView("/chooseCustomer");
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			String pactID = request.getParameter("ids");
			String[] pactIDArray = pactID.split(",");
			for (int i = 0; i < pactIDArray.length; i++) {
				map.put("pactstage", "2");
				map.put("pactID", pactIDArray[i]);
				map.put("user_Id", Util.getUserIdFromSession(request));
				pactService.djPact(map);
			}
			mv.addObject(SUCCESS, "true");
		} catch (Exception e) {
			mv.addObject(ERROR, "false");
		}
		return mv;
	}
	
	
	public ModelAndView updatePact(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();

			map.put("customerID", request.getParameter("customerID"));
			map.put("pactstage", request.getParameter("pactstage"));
			map.put("pactID", request.getParameter("pactID"));
			map.put("pactstage", request.getParameter("pactstage"));
			
			if (request.getParameter("customerName") != null) {
				map.put("customerName", request.getParameter("customerName"));
			}

			if (request.getParameter("customerArea") != null) {
				map.put("customerArea", request.getParameter("customerArea"));
			}
			if (request.getParameter("pactType") != null) {
				map.put("pactType", request.getParameter("pactType"));
			}

			if ("".equals(request.getParameter("pactMoney"))) {
				map.put("pactMoney", 0);
			} else {
				map.put("pactMoney", request.getParameter("pactMoney"));
			}
			if (request.getParameter("pactRemark") != null) {
				map.put("pactRemark", request.getParameter("pactRemark"));
			}
			if (request.getParameter("pactSuccessName") != null) {
				map.put("pactSuccessName", request
						.getParameter("pactSuccessName"));
			}

			if (request.getParameter("pactMoneyType") != null) {
				map.put("pactMoneyType", request.getParameter("pactMoneyType"));
			}
			if (request.getParameter("pactName") != null) {
				map.put("pactName", request.getParameter("pactName"));
			}
			if (request.getParameter("pactEndDate").equals("")) {
			} else {
				map.put("pactEndDate", request.getParameter("pactEndDate"));
			}
			if (request.getParameter("pactStartDate").equals("")) {
			} else {
				map.put("pactStartDate", request.getParameter("pactStartDate"));
			}
			if (request.getParameter("pactSuccessDate").equals("")) {
			} else {
				map.put("pactSuccessDate", request
						.getParameter("pactSuccessDate"));
			}
			map.put("LastUpdateBy", Util.getUserIdFromSession(request));
			map.put("LastUpdateDate", new SimpleDateFormat("yyyy-MM-dd")
					.format(date));
			pactService.updatePact(map);
			
			mv.addObject(SUCCESS, Boolean.TRUE);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(SUCCESS, "false");
		}
		return mv;
	}
	
	
	public ModelAndView searchPact(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		try {
			Map<String, Object> map = new HashMap<String, Object>();

			if (request.getParameter("searchName") != null) {
				map.put("searchName", request.getParameter("searchName"));
			}
			if (request.getParameter("pactstage") != null) {
				map.put("pactstage", request.getParameter("pactstage"));
			}
			String start = request.getParameter("start");// 起始行
			String limit = request.getParameter("limit");// 结束行
			start = start == null ? "0" : start;
			limit = limit == null ? "10" : limit;
			int beginRow = Integer.parseInt(start);
			int endRow = Integer.parseInt(limit); //+ beginRow;
			map.put("beginRow", beginRow);
			map.put("endRow", endRow);
			
			
			mv.addObject("result", pactService.searchPact(map));
			mv.addObject("total", pactService.searchPactCount(map));
			
			mv.addObject(SUCCESS, Boolean.TRUE);
			
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(SUCCESS, Boolean.FALSE);
		}
		return mv;
	}
	

	public ModelAndView addPact(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();

			map.put("customerID", request.getParameter("customerID"));
			map.put("pactstage", request.getParameter("pactstage"));

			if (request.getParameter("customerName") != null) {
				map.put("customerName", request.getParameter("customerName"));
			}

			if (request.getParameter("customerArea") != null) {
				map.put("customerArea", request.getParameter("customerArea"));
			}
			if (request.getParameter("pactType") != null) {
				map.put("pactType", request.getParameter("pactType"));
			}

			if ("".equals(request.getParameter("pactMoney"))) {
				map.put("pactMoney", 0);
			} else {
				map.put("pactMoney", request.getParameter("pactMoney"));
			}
			if (request.getParameter("pactRemark") != null) {
				map.put("pactRemark", request.getParameter("pactRemark"));
			}
			if (request.getParameter("pactSuccessName") != null) {
				map.put("pactSuccessName", request
						.getParameter("pactSuccessName"));
			}

			if (request.getParameter("pactMoneyType") != null) {
				map.put("pactMoneyType", request.getParameter("pactMoneyType"));
			}
			if (request.getParameter("pactName") != null) {
				map.put("pactName", request.getParameter("pactName"));
			}
			if (request.getParameter("pactEndDate").equals("")) {
			} else {
				map.put("pactEndDate", request.getParameter("pactEndDate"));
			}
			if (request.getParameter("pactStartDate").equals("")) {
			} else {
				map.put("pactStartDate", request.getParameter("pactStartDate"));
			}
			if (request.getParameter("pactSuccessDate").equals("")) {
			} else {
				map.put("pactSuccessDate", request
						.getParameter("pactSuccessDate"));
			}
			map.put("CreateBy", Util.getUserIdFromSession(request));
			map.put("CreateTime", new SimpleDateFormat("yyyy-MM-dd")
					.format(date));
			pactService.addPact(map);
			mv.addObject(SUCCESS, Boolean.TRUE);

		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(SUCCESS, "false");
		}
		return mv;

	}

	public void setPactService(PactService pactService) {
		this.pactService = pactService;
	}

}
